/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-08-27 14:02:12
 * @LastEditors: ziqi jhzq12345678
 * @LastEditTime: 2025-01-21 09:58:18
 * @FilePath: /pornhub_app/lib/src/modules/search/result/search_blogger_result.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:pornhub_app/components/keep_alive_wrapper.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/no_more/no_more.dart';

import 'package:pornhub_app/http/http_service.dart';

import 'package:pornhub_app/model/community/community_model.dart';
import 'package:pornhub_app/model/search/search_res_model.dart';

import 'package:pornhub_app/views/search/search_controller.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

class SearchDynamicResult extends StatefulWidget {
  final int searchType;
  const SearchDynamicResult({super.key, required this.searchType});

  @override
  State<SearchDynamicResult> createState() => _SearchDynamicResult();
}

class _SearchDynamicResult extends State<SearchDynamicResult> {
  final _pageSize = 20;
  final _scrollController = ScrollController();
  final PagingController<int, CommunityModel> _pagingController =
      PagingController(firstPageKey: 1);
  final searchvc = Get.find<SearchPageController>();
  Future _initListData(int pageKey) async {
    final dynamic = await httpInstance.get<SearchResModel>(
        url: 'search/keyWord',
        queryMap: {
          'pageSize': _pageSize,
          'searchType': widget.searchType,
          'searchWord': searchvc.keyWord,
          'page': pageKey,
        },
        complete: SearchResModel.fromJson);
    final isLastPage = dynamic.dynamicList.length < _pageSize;
    if (isLastPage) {
      _pagingController.appendLastPage(dynamic.dynamicList);
    } else {
      final nextPageKey = pageKey + 1;
      _pagingController.appendPage(dynamic.dynamicList, nextPageKey as int?);
    }
  }

  @override
  void initState() {
    _pagingController.addPageRequestListener((pageKey) {
      _initListData(pageKey);
    });
    ever(searchvc.keyWord, (v) {
      _pagingController.refresh();
    });
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return KeepAliveWrapper(
      child: RefreshIndicator(
        onRefresh: () => Future.sync(
          () => _pagingController.refresh(),
        ),
        child: PagedListView<int, CommunityModel>(
          scrollController: _scrollController,
          pagingController: _pagingController,
          physics: const AlwaysScrollableScrollPhysics(),
          builderDelegate: PagedChildBuilderDelegate<CommunityModel>(
            noMoreItemsIndicatorBuilder: (_) {
              return const NoMore();
            },
            noItemsFoundIndicatorBuilder: (_) {
              return const NoData();
            },
            itemBuilder: (context, item, index) => Container(
              margin: EdgeInsets.only(top: 10.w, bottom: 10.w),
              child: Container(),
              // child: CommunityCell(
              //   data: item,
              //   keyWord: searchvc.keyWord.value,
              // ),
            ),
          ),
        ),
      ),
    );
  }
}
